package com.longer.demo.biz.service.impl;

import com.longer.demo.biz.domain.ItemDO;
import com.longer.demo.biz.domain.ItemStockDO;
import com.longer.demo.biz.repository.ItemDOMapper;
import com.longer.demo.biz.repository.ItemStockDOMapper;
import com.longer.demo.biz.service.model.ItemModel;
import com.longer.demo.biz.service.ItemService;
import com.longer.demo.framework.error.BusinessError;
import com.longer.demo.framework.error.BusinessException;
import com.longer.demo.framework.validator.MyValidator;
import com.longer.demo.framework.validator.ValidationResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: mybatis
 * @description:
 * @author: SunBiaoLong
 * @create: 2019-01-14 11:49
 **/
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired(required = false)
	private ItemDOMapper itemDOMapper;
	@Autowired(required = false)
	private ItemStockDOMapper itemStockDOMapper;

	@Autowired
	private MyValidator validator;

	@Override
	public ItemModel getItemById(Integer id) {
		ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id);
		if (itemDO == null) {
			return null;
		}
		ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
		return convertFromDataObject(itemDO, itemStockDO);
	}


	@Override
	public List<ItemModel> getItemList() {
		List<ItemDO> itemDOList = itemDOMapper.getAll();
		return itemDOList.stream().map(
				itemDO -> convertFromDataObject(itemDO, itemStockDOMapper.selectByItemId(itemDO.getId())))
				.collect(Collectors.toList());
	}

	@Override
	@Transactional
	public ItemModel createItem(ItemModel itemModel) throws BusinessException {
		if (itemModel == null) {
			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR);
		}
		ValidationResult validationResult = validator.validate(itemModel);
		if (validationResult.isHasErrors()) {
			throw new BusinessException(BusinessError.PARAMETER_VALIDATION_ERROR, validationResult.getErrorMsg());
		}
		ItemDO itemDO = convertFromItemModel(itemModel);
		try {
			itemDOMapper.insertSelective(itemDO);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		itemModel.setId(itemDO.getId());
		System.out.println("qqqq:" + itemDO.getId());
		ItemStockDO itemStockDO = convertFromItemModelToItemStock(itemModel);
		itemStockDOMapper.insertSelective(itemStockDO);
		return null;
	}

	@Override
	@Transactional
	public void increaseSales(Integer account,Integer id) {
		itemDOMapper.increaseSales(account, id);
	}

	@Override
	public boolean minusStock(Integer account, Integer itemId) throws BusinessException {
		return itemStockDOMapper.minusStock(account, itemId) == 1;
	}

	private ItemDO convertFromItemModel(ItemModel itemModel) {
		if (itemModel == null) {
			return null;
		}
		ItemDO itemDO = new ItemDO();
		BeanUtils.copyProperties(itemModel, itemDO);
		itemDO.setPrice(itemModel.getPrice().doubleValue());
		return itemDO;
	}

	private ItemStockDO convertFromItemModelToItemStock(ItemModel itemModel) {
		if (itemModel == null) {
			return null;
		}
		ItemStockDO itemStockDO = new ItemStockDO();
		itemStockDO.setItemId(itemModel.getId());
		itemStockDO.setStock(itemModel.getStock());
		return itemStockDO;
	}

	private ItemModel convertFromDataObject(ItemDO itemDO, ItemStockDO itemStockDO) {
		if (itemDO == null) {
			return null;
		}
		ItemModel itemModel = new ItemModel();
		BeanUtils.copyProperties(itemDO, itemModel);
		itemModel.setPrice(new BigDecimal(itemDO.getPrice()));
		itemModel.setStock(itemStockDO.getStock());
		return itemModel;
	}
}
